﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Ongoal.Quotation;
using Ongoal.Quotation.Service.Common;
using Ongoal.Quotation.Service.Interface;
using Ongoal.Quotation.Service.Model;
using Ongoal.Quotation.Service.Param;
using Ongoal.Quotation.Service.ViewModel;

namespace Ongoal.Quotation.WebAPI.Controllers
{
    /// <summary>
    /// 用户管理相关接口
    /// </summary>
    public class UserController : WebAPIBase
    {
        /// <summary>
        /// 实现类接口
        /// </summary>
        private IUserService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public UserController(IUserService service)
        { 
            _service = service;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult AddUser([FromBody]ViewUser user)
        {
            //解密
            user.UserName = Des.Decrypt(user.UserName, Des.DefaultKEY);
            user.Password = Des.Decrypt(user.Password, Des.DefaultKEY);
            //解码
            user.UserName = System.Net.WebUtility.UrlDecode(user.UserName).Trim();
            user.Password = System.Net.WebUtility.UrlDecode(user.Password).Trim();

            string errorMsg = null;
            var result = _service.AddUser(user, ref errorMsg);
            if(result)
            {
                return ApiResult.ToSuccess(null);
            }
            else
            {
                return ApiResult.ToFail(errorMsg);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userList"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult Delete([FromBody] List<int> userList)
        {
            string errorMsg = null;
            var result = _service.Delete(userList, ref errorMsg);
            if (result)
            {
                return ApiResult.ToSuccess(null);
            }
            else
            {
                return ApiResult.ToFail(errorMsg);
            }
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult EditUser([FromBody] ViewUser user)
        {
            //解密
            user.UserName = Des.Decrypt(user.UserName, Des.DefaultKEY);
            user.Password = Des.Decrypt(user.Password, Des.DefaultKEY);
            //解码
            user.UserName = System.Net.WebUtility.UrlDecode(user.UserName).Trim();
            user.Password = System.Net.WebUtility.UrlDecode(user.Password).Trim();

            string errorMsg = null;
            var result = _service.EditUser(user, ref errorMsg);
            if (result)
            {
                return ApiResult.ToSuccess(null);
            }
            else
            {
                return ApiResult.ToFail(errorMsg);
            }
        }

        /// <summary>
        /// 分页获取用户
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="usertype"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getuserbypage(int pagesize, int pageindex, int usertype, int orgid, string keyword) {
            PaginationData PaginationEntity = new PaginationData { PageIndex = pageindex, PageSize = pagesize };
            return _service.Getuserbypage(PaginationEntity, usertype, orgid, keyword);
        }

        /// <summary>
        /// 获取用户对应的组织机构信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetUserOrg(int userID)
        {
            return _service.GetUserOrg(userID);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="updateInfo"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        public dynamic Updateloginpwd([FromBody] dynamic updateInfo)
        {
            var odlpwd = updateInfo.odlpwd != null ? updateInfo.odlpwd.Value : "";
            var newstr = updateInfo.newstr != null ? updateInfo.newstr.Value : "";
            if (string.IsNullOrEmpty(odlpwd) || string.IsNullOrEmpty(newstr))
            {
                throw new Exception("请提供对应的参数");
            }

            //解密
            odlpwd = Des.Decrypt(odlpwd, Des.DefaultKEY);
            newstr = Des.Decrypt(newstr, Des.DefaultKEY);

            //解码
            odlpwd = System.Net.WebUtility.UrlDecode(odlpwd).Trim();
            newstr = System.Net.WebUtility.UrlDecode(newstr).Trim();

            return _service.UpdatePwd(odlpwd, newstr);
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        /// <param name="avatarID"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        public dynamic UpdateAvatar([FromBody] dynamic avatarID)
        {
            return _service.UpdateAvatar(avatarID.avatarID.Value);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetUserInfo() {
            return _service.GetUserInfo();
        }

        #region 根据多个机构Id集合获取多个机构下人员
        /// <summary>
        /// 根据多个机构Id集合获取多个机构下人员
        /// </summary>
        /// <param name="Param">参数（）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> GetOrgUserList(PublicParam Param)
        {
            return await _service.GetOrgUserList(Param);
        }
        #endregion

        #region 根据多个人员Id集合获取多个机构
        /// <summary>
        /// 根据多个人员Id集合获取多个机构
        /// </summary>
        /// <param name="Param">参数（）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> GetUserOrgList(PublicParam Param)
        {
            return await _service.GetUserOrgList(Param);
        }
        #endregion

        #region 获取所有用户信息
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> GetUserList()
        {
            return await _service.GetUserList();
        }
        #endregion

        #region 获取组织与人员
        /// <summary>
        /// 获取组织与人员
        /// </summary>
        /// <param name="Param">参数（）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> GetOrgAndUser(PublicParam Param)
        {
            return await _service.GetOrgAndUser(Param);
        }
        #endregion
    }
}
